# Input:    /Data/finalData.RData
# Author:   JB


rm(list = ls())
require(tidyverse)
require(ggridges)
require(ggrepel)

setwd('C:/Users/Jimbo/Dropbox/FED/FED/Paper/JOP/RR1_replication/')

load('./data/finalData.RData')


toplot2 <- utterance_level %>%
  arrange(docID,ind) %>%
  mutate(interruptor2 = ifelse(interruptor == 1 & lag(interrupted,2) == 0,1,0)) %>%
  mutate(fed = ifelse(fed == 0 & GOP == 1 & Male == 1,'GOP - Male',
                      ifelse(fed == 0 & GOP == 1 & Male == 0,'GOP - Female',
                             ifelse(fed == 0 & DEM == 1 & Male == 1,'DEM - Male',
                                    ifelse(fed == 0 & DEM == 1 & Male == 0,'DEM - Female',
                                           ifelse(fed == 0 & GOP == 0 & DEM == 0,'Expert','Fed Chair')))))) %>%
  filter(fed != 'Expert') %>%
  mutate(fed = ifelse(grepl('Male',fed),'Male',
                      ifelse(grepl('Female',fed),'Female',fed))) %>%
  group_by(opensecretsID,fed) %>%
  summarise(propInterrupted = mean(interrupted),
            propInterruptor = mean(interruptor),
            propInterruptor2 = mean(interruptor2),
            n = n()) %>%
  ungroup() %>%
  mutate(mult = propInterrupted / propInterruptor,
         mult2 = propInterrupted / propInterruptor2) %>%
  filter(n > 20)  %>%
  select(-matches('mult')) %>%
  pivot_longer(cols = c('propInterruptor','propInterruptor2')) %>%
  mutate(mult = propInterrupted/value)

pdf('./output/figures/interruptScatter.pdf',width = 7,height = 5)
toplot2 %>%
  filter(name == 'propInterruptor2') %>%
  ggplot(aes(x = propInterrupted,y = value,size = n,color = fed == 'Fed Chair')) + 
  geom_point() + 
  scale_color_manual(name = 'Speaker',values = c('grey60','black')) + 
  geom_abline(slope = 1,intercept = 0) + 
  geom_text_repel(data = toplot2 %>% filter(grepl('FED',opensecretsID),
                                            name == 'propInterruptor2'),aes(label = paste0(gsub('FED','',opensecretsID),' (x',
                                                                                           round(mult,1),')')),
                  size = 3,color = 'black',min.segment.length = 0,point.padding = .5,box.padding = .5) + 
  scale_x_continuous(labels = scales::percent,limits = c(0,.3)) + 
  scale_y_continuous(labels = scales::percent,limits = c(0,.3)) + 
  labs(title = 'Interrupted vs. Interrupting',
       size = '# Utterances',
       x = '% Interrupted',
       y = '% Interrupting') + 
  theme_bw()
dev.off()